package com.hengtongxing.hejiayun.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.MyHouseListBean;
import com.hengtongxing.hejiayun.event.OrderRefushEvent;
import com.hengtongxing.hejiayun.homepage.adapter.PayHouseListAdapter;
import com.hengtongxing.hejiayun.mine.model.IMineModel;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun.widget.AppTitleBar;
import com.hengtongxing.hejiayun.widget.WrapContentLinearLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayFeeActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<MyHouseListBean.DataBean> beanList;
    private PayHouseListAdapter listAdapter;
    private IMineModel mineModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_fee);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
        initData();
    }

    @Subscribe
    public void updata(OrderRefushEvent event) {
        beanList.clear();
        requestMyHouse();
        refreshLayout.finishRefresh(30000);
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        mineModel = new MineModelImpl();
        titleBar.setRightClickListener(() -> {
            showActivity(PayFeeListActivity.class);
        });
    }


    public void initData() {
        beanList = new ArrayList<>();
        listAdapter = new PayHouseListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener(position -> {
            MyHouseListBean.DataBean dataBean = beanList.get(position);
            Intent intent = new Intent(mActivity, AddPayFeeActivity.class);
            intent.putExtra("house_id", dataBean.getHid() + "");
            intent.putExtra("house_name", dataBean.getComm_name() + dataBean.getBuilding() + dataBean.getUnit_name() + dataBean.getFloor() + dataBean.getHose_number());
            startActivity(intent);
        });
        requestMyHouse();
    }

    public void requestMyHouse() {
        mineModel.requestMyHouse(new DataCallBack<MyHouseListBean>() {
            @Override
            public void onSuccessful(MyHouseListBean bean) {
                if (bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                refreshLayout.finishRefresh();
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        beanList.clear();
        requestMyHouse();
        refreshLayout.finishRefresh(30000);
    }
}
